﻿using B.S.BasicData.Api.Read.Application.Command.GridPerson;
using B.S.BasicData.Api.Read.DTOs;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.GridPerson
{
    /// <summary>
    /// 查询网格人员(Account)分页列表的处理类
    /// </summary>
    public class GetGridPersonHandler : IRequestHandler<GetGridPersonCommand, ApiPaging<AccountListDTO>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;
        private readonly IBaseRepository<AccountRoleModel> accountRoleRepo;
        private readonly IBaseRepository<RoleModel> roleRepo;
        private readonly IBaseRepository<DepartmentModel> departRepo;
        private readonly ILogger<GetGridPersonHandler> logger;

        //注入仓储
        public GetGridPersonHandler(IBaseRepository<AccountModel> accountRepo, IBaseRepository<AccountRoleModel> accountRoleRepo, IBaseRepository<RoleModel> roleRepo,IBaseRepository<DepartmentModel> departRepo,ILogger<GetGridPersonHandler> logger)
        {
            this.accountRepo = accountRepo;
            this.accountRoleRepo = accountRoleRepo;
            this.roleRepo = roleRepo;
            this.departRepo = departRepo;
            this.logger = logger;
        }

        public async Task<ApiPaging<AccountListDTO>> Handle(GetGridPersonCommand request, CancellationToken cancellationToken)
        {
            
            ApiPaging < AccountListDTO > res =new ApiPaging<AccountListDTO> ();

            //把所有的实体列表表达式列出
            var accounts = accountRepo.GetAll().Where(x => x.IsDeleted == false); //select * from xxx表
            var roles = roleRepo.GetAll().Where (x => x.IsDeleted == false&&x.Id !=1&& x.Id != 2);//排除掉管理员
            var accountRoles = accountRoleRepo.GetAll().Where (y => y.IsDeleted == false);
            var departments = departRepo.GetAll().Where(x=>!x.IsDeleted);


            //过滤条件判断:1.账号名称(模糊查询) 2.部门Id(精确查询)
            if (!string.IsNullOrEmpty(request.RealName)) {
                accounts = accounts.Where(x=>x.RealName.Contains(request.RealName));
            }
            if (request.DepartmentId!=null)
            {
                departments = departments.Where(x => x.Id==request.DepartmentId);
            }

            //linq 组合查询

            var linq = from a in accounts
                       join ar in accountRoles on a.Id equals ar.AccountId  //账号跟账号角色的关联
                       join r in roles on ar.RoleId equals r.Id  //角色跟账号角色的关联
                       join d in departments on a.DepartmentId equals d.Id //账号里部门id跟部门的id 关联
                       select new AccountListDTO
                       {
                           Id = a.Id,
                           AccountName = a.AccountName,
                           CardId = a.CardId,
                           DepartmentName = d.DepartmentName,
                           //Img= a.Img,
                           PhoneNo = a.PhoneNo,
                           RealName = a.RealName,
                           RoleName = r.RoleName,
                           DepartmentId = a.DepartmentId,
                           RoleId = r.Id
                       };

            res.TotalCount = linq.Count();//必须是linq 表达式对应的数据的总条数
            res.Msg = "分页查询成功";
            res.PageDatas = linq.OrderByDescending(x=>x.Id).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            res.Code = ApiEnum.Success;

            return res;
        }
    }
}
